<template>
	<view class="container top">
		<view class="form-group">
			<view class="form-item">
				<text class="label">标题：</text>
				<input type="text" v-model="form.title" placeholder="请输入活动标题" class="input"/>
			</view>
			
			<view class="form-item">
				<text class="label">内容：</text>
				<textarea v-model="form.content" placeholder="请输入活动内容" class="textarea"/>
			</view>
			
			<view class="form-item">
				<text class="label">图片/视频：</text>
				<view class="upload-area">
					<view class="image-item" v-if="form.image">
						<image :src="form.image" mode="aspectFill"></image>
						<text class="delete-btn" @click="deleteImage">×</text>
					</view>
					<view class="upload-btn" @click="chooseImage" v-else>
						<text class="plus">+</text>
					</view>
				</view>
			</view>
		</view>
		
		<view class="submit-btn" @click="handleSubmit">
			发布
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';

const form = ref({
	title: '',
	content: '',
	image: '',
	createTime: ''
});

function chooseImage() {
	uni.chooseImage({
		count: 1,
		success: (res) => {
			form.value.image = res.tempFilePaths[0];
		}
	});
}

function deleteImage() {
	form.value.image = '';
}

function handleSubmit() {
	if (!form.value.title || !form.value.content) {
		uni.showToast({
			title: '请填写完整信息',
			icon: 'none'
		});
		return;
	}
	
	// 获取当前时间
	const now = new Date();
	form.value.createTime = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
	
	// 获取之前的活动列表
	let activities = uni.getStorageSync('activities') || [];
	activities.unshift({
		...form.value,
		status: '审核中' // 新发布的活动默认为审核中状态
	});
	
	// 保存到本地存储
	uni.setStorageSync('activities', activities);
	
	uni.showToast({
		title: '发布成功',
		icon: 'success'
	});
	
	setTimeout(() => {
		uni.navigateBack();
	}, 1500);
}
</script>

<style>
.container {
	padding: 30rpx;
	background-color: #fff;
	min-height: 100vh;
}

.form-group {
	margin-bottom: 40rpx;
	background-color: #fff;
	width: 700rpx;
	border-radius: 24rpx;
}

.form-item {
	margin-bottom: 30rpx;
}
.top {
		width: 100%;
		height: 400rpx;
		background-image: url(/static/xiao@3x.png);
		background-repeat: no-repeat;
		background-size: 100%;
	}

.label {
	display: block;
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
}

.input {
	width: 690rpx;
	height: 80rpx;
	border: 1rpx solid #eee;
	border-radius: 8rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
}

.textarea {
	width: 100%;
	height: 300rpx;
	border: 1rpx solid #eee;
	border-radius: 8rpx;
	padding: 20rpx;
	font-size: 28rpx;
}

.upload-area {
	display: flex;
	flex-wrap: wrap;
	gap: 20rpx;
}

.image-item {
	width: 200rpx;
	height: 200rpx;
	position: relative;
}

.image-item image {
	width: 100%;
	height: 100%;
	border-radius: 8rpx;
}

.delete-btn {
	position: absolute;
	right: -16rpx;
	top: -16rpx;
	width: 40rpx;
	height: 40rpx;
	line-height: 40rpx;
	text-align: center;
	background-color: rgba(0, 0, 0, 0.5);
	color: #fff;
	border-radius: 50%;
	font-size: 32rpx;
}

.upload-btn {
	width: 200rpx;
	height: 200rpx;
	background-color: #f5f5f5;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 8rpx;
}

.plus {
	font-size: 60rpx;
	color: #999;
}

.submit-btn {
	position: fixed;
	bottom: 40rpx;
	left: 40rpx;
	right: 40rpx;
	height: 88rpx;
	line-height: 88rpx;
	text-align: center;
	background-color: #007AFF;
	color: #fff;
	border-radius: 44rpx;
	font-size: 32rpx;
}
</style> 